package com.itheima.shop.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.shop.entity.Product;
import com.itheima.shop.mapper.ProductMapper;
import com.itheima.shop.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService{
	@Autowired
	private ProductMapper mapper;
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
	@Override
	public PageInfo<Product> findHot() {
		PageInfo<Product> hotProductsPage=null;
		ValueOperations<String, String> ops=redisTemplate.opsForValue();
		String hotProductStr=ops.get("hotProducts");
		try{
			if(hotProductStr!=null){
//				List<Product> hotProducts=JSON.parseArray(hotProductStr, Product.class);
//				hotProductsPage=PageInfo.of(hotProducts);
				hotProductsPage=JSON.parseObject(hotProductStr, new TypeReference<PageInfo<Product>>(){});
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		if(hotProductsPage==null){
			PageHelper.startPage(1,9,false);
			Page<Product> hotProducts=mapper.findHot();
//			hotProductsPage=PageInfo.of(hotProducts);
//			String redisHotProductStr=JSON.toJSONString(hotProducts);
//			ops.set("hotProducts", redisHotProductStr, 100L, TimeUnit.SECONDS);
			hotProductsPage=PageInfo.of(hotProducts);
			String redisHotProductStr=JSON.toJSONString(hotProductsPage);
			ops.set("hotProducts", redisHotProductStr,150L,TimeUnit.SECONDS);
		}
		return hotProductsPage;
	}

	@Override
	public PageInfo<Product> findByCid(int cid,int pageNum,int pageSize,String orderBy) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum,pageSize,orderBy);
		return PageInfo.of(mapper.findByCid(cid));
	}

	@Override
	public Product findByPid(int pid) {
		// TODO Auto-generated method stub
		return mapper.findByPid2(pid);
	}

	public PageInfo<Product> findNew() {
		PageInfo<Product> newProductsPage=null;
		ValueOperations<String, String> ops=redisTemplate.opsForValue();
		String newProductStr=ops.get("newProducts");
		try{
			if(newProductStr!=null){
//				List<Product> newProduct=JSON.parseArray(newProductStr, Product.class);
//				newProductsPage=PageInfo.of(newProduct);
				newProductsPage=JSON.parseObject(newProductStr, new TypeReference<PageInfo<Product>>(){});
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		if(newProductsPage==null){
			PageHelper.startPage(1,9,false);
			PageHelper.orderBy("pdate desc");
			Page<Product> newProducts=mapper.findNew();
//			newProductsPage=PageInfo.of(newProducts);
//			String redisNewProductStr=JSON.toJSONString(newProducts);
//			ops.set("newProducts", redisNewProductStr, 100L, TimeUnit.SECONDS);
			newProductsPage=PageInfo.of(newProducts);
			String redisNewProductStr=JSON.toJSONString(newProductsPage);
			ops.set("newtProducts", redisNewProductStr,15L,TimeUnit.SECONDS);
		}
		return newProductsPage;
	}
	
}
